<?php
include_once "DBCORE.cls.php";
	class db_promotion
	{
		private $tablename="tblpromotion";
		private $db;
		public function __construct()
		{
			$this->db= new DBCORE();
			$this->db->db_connect('localhost','root','','restaurant');
			$this->db->db_query("SET NAMES 'UTF8'");
		}
		
		/*
		*
		SELECT DATE_FORMAT(birthday_date, '%d %m% %Y')
		FROM table_user
		hoặc bạn có thể viết như sau
		SELECT DATE_FORMAT(birthday_date, '%d-%m%-%Y')
		FROM table_user 
		*
		$ts = strtotime('22-12-2012');
		echo $ts . "<br />";
		echo date('d/m/Y', $ts);
		?>
		*
		*/
		public function select($id)
		{
			$id = !Empty($id) ? " WHERE PromotionID='{$id}'": "";
			$myStrSQL = "SELECT * FROM {$this->tablename} {$id}";
			$data = $this->db->db_fetch_assoc($this->db->db_query($myStrSQL));
			return $data;
		}
		
		public function selectAll($id)
		{
			if(empty($id))
				return false;
			$myStrSQL="SELECT * FROM {$this->tablename} WHERE RestaurantID ='$id'";
			$result=$this->db->db_query($myStrSQL);
			$resArray = array();
			while ($row = $this->db->db_fetch_assoc($result)) {
				$resArray[] = $row;
			}
			return $resArray;
		}
		//
		public function delete($id)
		{
			$id = !Empty($id) ? " WHERE PromotionID='{$id}'": "";
			$myStrSQL="DELETE FROM {$this->tablename} {$id}";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		public function deleteAll()
		{
			$myStrSQL="DELETE FROM {$this->tablename} WHERE 1";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		//
		public function insert(array $data)
		{
			$id = isset($data['RestaurantID'])?$data['RestaurantID']:"";
			if(empty($id))
				return false;
			$Information = $data['Information'];
			$Discount = $data['Discount'];
			$DayStart = $data['DayStart'];
			$DayEnd = $data['DayEnd'];
			$myStrSQL = "INSERT INTO {$this->tablename} (RestaurantID, Information, Discount, DayStart, DayEnd, Status) VALUES 
			('$id','$Information','$Discount','$DayStart','$DayEnd','false')";
			return ($this->db->db_query($myStrSQL));
		}
		
		//
		public function updates($update = array(), $condition)
		{
			if(empty($condition) || !checkExists($condition))
				return false;
			//PromotionID = '{$update['PromotionID']}',
			$update = "Information = '{$update['Information']}',
					   Discount = '{$update['Discount']}',
					   DayStart = '{$update['DayStart']}',
					   DayEnd = '{$update['DayEnd']}',
					   Status = '{$update['Status']}'";
			$condition = "PromotionID = '{$condition}'";
			$myStrSQL = "UPDATE {$this->tablename} SET {$update} WHERE {$condition}";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		
		//
		
		
		//
		public function checkExists($id='')
		{
			if(empty($id))
				return false;
			$myStrSQL = "SELECT * FROM {$this->tablename} WHERE RestaurantID ='{$id}'";
			return $this->db->db_query($myStrSQL);
			
		}
	}

?>